package com.xjtu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xjtu.dto.UserPageDto;
import com.xjtu.dto.serviceRecord.AddServiceRecordDto;
import com.xjtu.dto.serviceRecord.UpdateServiceDto;
import com.xjtu.entity.Elderly;
import com.xjtu.entity.ServiceRecord;
import com.xjtu.entity.User;
import com.xjtu.exception.BaseException;
import com.xjtu.mapper.ElderlyMapper;
import com.xjtu.mapper.ServiceRecordMapper;
import com.xjtu.mapper.UserMapper;
import com.xjtu.result.PageResult;
import com.xjtu.service.ServiceRecordService;
import com.xjtu.vo.UserVo;
import com.xjtu.vo.serviceRecord.ServiceRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ServiceRecordServiceImpl implements ServiceRecordService {
    @Autowired(required = false)
    private ServiceRecordMapper serviceRecordMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private ElderlyMapper elderlyMapper;

    @Override
    public ServiceRecordVo save(AddServiceRecordDto addServiceRecordDto) {
        log.info("传入的参数为{}",addServiceRecordDto);
        // 首先判断用户和老人是否存在
        User user = userMapper.selectByPrimaryKey(addServiceRecordDto.getUId());
        if(user == null){
            throw new BaseException("员工不存在");
        }
        Elderly elderly = elderlyMapper.selectByPrimaryKey(Long.valueOf(addServiceRecordDto.getElderlyId()));
        if(elderly == null){
            throw new BaseException("该老人不存在");
        }
        //  到这里可以进行插入了
        ServiceRecord serviceRecord = new ServiceRecord();
        BeanUtils.copyProperties(addServiceRecordDto,serviceRecord);
        if(serviceRecord.getCreateTime() == null){
            serviceRecord.setCreateTime(LocalDateTime.now());
        }
        log.info("待插入的数据为{}",serviceRecord);
        serviceRecordMapper.insert(serviceRecord);
        ServiceRecordVo resp = new ServiceRecordVo();
        BeanUtils.copyProperties(serviceRecord,resp);
        resp.setElderlyName(elderly.getName());
        resp.setElderlyId(elderly.getId());
        resp.setEmpName(user.getName());
        resp.setEmpId(user.getId());
        return resp;
    }

    @Override
    public ServiceRecordVo update(UpdateServiceDto updateServiceRecordDto) {
        // 还是要先判断用户和老人是否存在,还有这条记录
        ServiceRecord record = serviceRecordMapper.selectByPrimaryKey(Long.valueOf(updateServiceRecordDto.getId()));
        if (record == null){
            throw new BaseException("记录不存在");
        }
        User user = userMapper.selectByPrimaryKey(updateServiceRecordDto.getUId());
        if(user == null){
            throw new BaseException("员工不存在");
        }
        Elderly elderly = elderlyMapper.selectByPrimaryKey(Long.valueOf(updateServiceRecordDto.getElderlyId()));
        if(elderly == null){
            throw new BaseException("该老人不存在");
        }
        // 到这里记录存在了，进行修改
        ServiceRecord serviceRecord = new ServiceRecord();
        BeanUtils.copyProperties(updateServiceRecordDto,serviceRecord);
        serviceRecordMapper.updateByPrimaryKey(serviceRecord);

        // 进行返回
        ServiceRecordVo resp = new ServiceRecordVo();
        BeanUtils.copyProperties(serviceRecord,resp);
        resp.setElderlyName(elderly.getName());
        resp.setElderlyId(elderly.getId());
        resp.setEmpName(user.getName());
        resp.setEmpId(user.getId());
        return resp;
    }

    @Override
    public ServiceRecordVo queryById(Integer id) {
        ServiceRecord record = serviceRecordMapper.selectByPrimaryKey(Long.valueOf(id));
        // 先判断是不是空
        if(record == null){
            throw new BaseException("记录不存在");
        }
        // 不为空
        User user = userMapper.selectByPrimaryKey(record.getUId());
        Elderly elderly = elderlyMapper.selectByPrimaryKey(Long.valueOf(record.getElderlyId()));
        // 进行返回
        ServiceRecordVo resp = new ServiceRecordVo();
        BeanUtils.copyProperties(record,resp);
        resp.setElderlyName(elderly.getName());
        resp.setElderlyId(elderly.getId());
        resp.setEmpName(user.getName());
        resp.setEmpId(user.getId());
        return resp;
    }

    @Override
    public PageResult pageQuery(Integer page, Integer size, String name) {
        PageHelper.startPage(page, size);
        UserPageDto userPageDto = new UserPageDto();
        userPageDto.setPage(page);
        userPageDto.setSize(size);
        userPageDto.setName(name);
        log.info("传入的参数为{}",userPageDto);
        log.info("要查找的名字为{}",name);
        Page<ServiceRecord> userPage = serviceRecordMapper.pageQuery(name);
        long total = userPage.getTotal();
        List<ServiceRecord> records = userPage.getResult();
        List<ServiceRecordVo> respLists = new ArrayList<>();
        for(ServiceRecord record : records){
            User user = userMapper.selectByPrimaryKey(record.getUId());
            Elderly elderly = elderlyMapper.selectByPrimaryKey(Long.valueOf(record.getElderlyId()));
            // 进行返回
            ServiceRecordVo resp = new ServiceRecordVo();
            BeanUtils.copyProperties(record,resp);
            resp.setElderlyName(elderly.getName());
            resp.setElderlyId(elderly.getId());
            resp.setEmpName(user.getName());
            resp.setEmpId(user.getId());
            respLists.add(resp);
        }
        return new PageResult(total, respLists);
    }
}
